import json
import time
import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class TestVolcanoConfigService:
    @pytest.mark.parametrize('yaml_numa', YamlUtil().read_testcase_yaml(
        'v25.06/volcanoConfigService/GET_get_numa_aware_policy.yaml'))
    def test_get_numa_aware_policy(self, yaml_numa):
        # 获取NUMA亲和性调度策略
        uri = yaml_numa['request']['url']
        uri = uri.replace('{cluster}', datas.test_volcano_config_servcie['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_numa['request']['method']
        headers = yaml_numa['request']['headers']
        data = yaml_numa['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_numa['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验

    @pytest.mark.parametrize('yaml_numa', YamlUtil().read_testcase_yaml(
        'v25.06/volcanoConfigService/PUT_modify_numa_aware_policy.yaml'))
    def test_modify_numa_aware_policy(self, yaml_numa):
        # 修改NUMA亲和性调度策略
        uri = yaml_numa['request']['url']
        uri = uri.replace('{cluster}', datas.test_volcano_config_servcie['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_numa['request']['method']
        headers = yaml_numa['request']['headers']
        data = yaml_numa['request']['data']
        data['policy'] = datas.test_volcano_config_servcie['action_open']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_numa['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验

    @pytest.mark.parametrize('yaml_numa', YamlUtil().read_testcase_yaml(
        'v25.06/volcanoConfigService/GET_get_numa_distance_policy.yaml'))
    def test_get_numa_distance_policy(self, yaml_numa):
        # 获取NUMA Distance策略
        uri = yaml_numa['request']['url']
        uri = uri.replace('{cluster}', datas.test_volcano_config_servcie['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_numa['request']['method']
        headers = yaml_numa['request']['headers']
        data = yaml_numa['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_numa['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验

    @pytest.mark.parametrize('yaml_numa', YamlUtil().read_testcase_yaml(
        'v25.06/volcanoConfigService/PUT_modify_numa_distance_policy.yaml'))
    def test_modify_numa_distance_policy(self, yaml_numa):
        # 修改NUMA Distance策略
        uri = yaml_numa['request']['url']
        uri = uri.replace('{cluster}', datas.test_volcano_config_servcie['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_numa['request']['method']
        headers = yaml_numa['request']['headers']
        data = yaml_numa['request']['data']
        data['policy'] = datas.test_volcano_config_servcie['action_open']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_numa['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验

    @pytest.mark.parametrize('yaml_numa', YamlUtil().read_testcase_yaml(
        'v25.06/volcanoConfigService/GET_get_numa_fast_policy.yaml'))
    def test_get_numa_fast_policy(self, yaml_numa):
        # 获取NUMA Distance策略
        uri = yaml_numa['request']['url']
        uri = uri.replace('{cluster}', datas.test_volcano_config_servcie['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_numa['request']['method']
        headers = yaml_numa['request']['headers']
        data = yaml_numa['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_numa['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验

    @pytest.mark.parametrize('yaml_numa', YamlUtil().read_testcase_yaml(
        'v25.06/volcanoConfigService/PUT_modify_numa_fast_policy.yaml'))
    def test_modify_numa_fast_policy(self, yaml_numa):
        # 修改NUMA Distance策略
        uri = yaml_numa['request']['url']
        uri = uri.replace('{cluster}', datas.test_volcano_config_servcie['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_numa['request']['method']
        headers = yaml_numa['request']['headers']
        data = yaml_numa['request']['data']
        data['policy'] = datas.test_volcano_config_servcie['action_open']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_numa['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验

    @pytest.mark.parametrize('yaml_numa', YamlUtil().read_testcase_yaml(
        'v25.06/volcanoConfigService/PUT_set_volcano_scheduler_configmap_to_default.yaml'))
    def test_set_volcano_scheduler_configmap_to_default(self, yaml_numa):
        # 恢复ConfigMap默认配置
        uri = yaml_numa['request']['url']
        uri = uri.replace('{cluster}', datas.test_volcano_config_servcie['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_numa['request']['method']
        headers = yaml_numa['request']['headers']
        data = yaml_numa['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_numa['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验

    @pytest.mark.parametrize('yaml_numa', YamlUtil().read_testcase_yaml(
        'v25.06/volcanoConfigService/GET_get_node_list.yaml'))
    def test_get_node_list(self, yaml_numa):
        # 获取节点列表
        uri = yaml_numa['request']['url']
        uri = uri.replace('{cluster}', datas.test_volcano_config_servcie['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_numa['request']['method']
        headers = yaml_numa['request']['headers']
        data = yaml_numa['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_numa['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验

    @pytest.mark.parametrize('yaml_numa', YamlUtil().read_testcase_yaml(
        'v25.06/volcanoConfigService/GET_get_node_numaaware_topo_policy.yaml'))
    def test_get_node_numaaware_topo_policy(self, yaml_numa):
        # 获取numatopologies信息 (获取节点的numa调度拓扑策略)
        uri = yaml_numa['request']['url']
        uri = uri.replace('{cluster}', datas.test_volcano_config_servcie['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_numa['request']['method']
        headers = yaml_numa['request']['headers']
        data = yaml_numa['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_numa['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验

    @pytest.mark.parametrize('yaml_numa', YamlUtil().read_testcase_yaml(
        'v25.06/volcanoConfigService/GET_get_podname_from_uuid.yaml'))
    def test_get_podname_from_uuid(self, yaml_numa):
        # 根据UUID获取Pod名称
        uri = yaml_numa['request']['url']
        uri = uri.replace('{cluster}', datas.test_volcano_config_servcie['cluster_name'])
        # 暂时由配置文件指定。后续优化为临时部署一个pod，拿到uuid、调用接口后删掉pod
        uri = uri.replace('{uuid}', datas.test_volcano_config_servcie['uuid'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_numa['request']['method']
        headers = yaml_numa['request']['headers']
        data = yaml_numa['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_numa['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
